<template>
  <el-form :model="form" v-loading="loading">
    <el-form-item prop="name" label="角色名称:">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item prop="code" label="角色编码:">
      <el-input v-model="form.code" />
    </el-form-item>
    <el-form-item prop="description" label="描述:">
      <el-input v-model="form.description" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script lang="ts">
import Vue from 'vue'
import { wrapAsyncWithLoading } from '@/utils/helper'
import { getInfo, createOrUpdate } from '@/services/role'
export default Vue.extend({
  name: 'FormAddOrEditRole',
  props: ['id'],
  data () {
    return {
      loading: false,
      form: {
        id: null,
        name: 'test_lnw_',
        code: '',
        description: ''
      }
    }
  },
  created () {
    wrapAsyncWithLoading(this, 'loading', 'submit')
    wrapAsyncWithLoading(this, 'loading', 'loadData')
    this.loadData()
  },
  methods: {
    async loadData () {
      if (this.id) {
        const { data } = await getInfo(this.id)
        if (data.code === '000000') {
          this.form = data.data
          return
        }
        this.$emit('error', '获取信息失败，请重试')
      }
    },
    async submit () {
      const { data } = await createOrUpdate(this.form)
      if (data.code === '000000') {
        this.$emit('suc')
        return
      }
      this.$emit('fail')
    }
  }
})
</script>
